package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbMinuteBidBag;
import org.dromara.system.domain.dto.NumDTO;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbCandidateInfoBo;
import org.dromara.system.domain.vo.WbCandidateInfoVo;
import org.dromara.system.domain.WbCandidateInfo;
import org.dromara.system.mapper.WbCandidateInfoMapper;
import org.dromara.system.service.IWbCandidateInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 推荐中标候选人公示Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class WbCandidateInfoServiceImpl implements IWbCandidateInfoService {

    private final WbCandidateInfoMapper baseMapper;

    /**
     * 查询推荐中标候选人公示
     */
    @Override
    public WbCandidateInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询推荐中标候选人公示列表
     */
    @Override
    public TableDataInfo<WbCandidateInfoVo> queryPageList(WbCandidateInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbCandidateInfo> lqw = buildQueryWrapper(bo);
        lqw.orderByAsc(WbCandidateInfo::getStatus);
        lqw.orderByDesc(WbCandidateInfo::getPublicityTime);
        lqw.select(WbCandidateInfo.class, info -> !info.getColumn().equals("content"));// 分页查询排除该列
        Page<WbCandidateInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询推荐中标候选人公示列表
     */
    @Override
    public List<WbCandidateInfoVo> queryList(WbCandidateInfoBo bo) {
        LambdaQueryWrapper<WbCandidateInfo> lqw = buildQueryWrapper(bo);
        lqw.select(WbCandidateInfo.class, info -> !info.getColumn().equals("content"));// 分页查询排除该列
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbCandidateInfo> buildQueryWrapper(WbCandidateInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbCandidateInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPurchaseNo() != null, WbCandidateInfo::getPurchaseNo, bo.getPurchaseNo());
        lqw.like(StringUtils.isNotBlank(bo.getHeadline()), WbCandidateInfo::getHeadline, bo.getHeadline());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), WbCandidateInfo::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getTenderee()), WbCandidateInfo::getTenderee, bo.getTenderee());
        lqw.eq(StringUtils.isNotBlank(bo.getYear()), WbCandidateInfo::getYear, bo.getYear());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), WbCandidateInfo::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getTenderAgent()), WbCandidateInfo::getTenderAgent, bo.getTenderAgent());
        lqw.eq(bo.getPublicityTime() != null, WbCandidateInfo::getPublicityTime, bo.getPublicityTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDissentPhone()), WbCandidateInfo::getDissentPhone, bo.getDissentPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getDissentEmail()), WbCandidateInfo::getDissentEmail, bo.getDissentEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WbCandidateInfo::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增推荐中标候选人公示
     */
    @Override
    public Boolean insertByBo(WbCandidateInfoBo bo) {
        bo.setStatus("0");
        WbCandidateInfo add = MapstructUtils.convert(bo, WbCandidateInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改推荐中标候选人公示
     */
    @Override
    public Boolean updateByBo(WbCandidateInfoBo bo) {
        WbCandidateInfo update = MapstructUtils.convert(bo, WbCandidateInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbCandidateInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除推荐中标候选人公示
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean issueCandidate(List<Long> ids) {

        return baseMapper.update(new LambdaUpdateWrapper<WbCandidateInfo>().set(WbCandidateInfo::getStatus,"0")
            .in(WbCandidateInfo::getId,ids))> 0;
    }

    @Override
    public List<NumDTO> countNum() {
        return baseMapper.selectCountNum();
    }

    @Override
    public Boolean withdraw(List<Long> ids) {
        return updateStatus(ids, "-1");
    }

    @Override
    public List<WbCandidateInfoVo> queryListByName(WbCandidateInfoBo bo) {
        LambdaQueryWrapper<WbCandidateInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getHeadline()),WbCandidateInfo::getHeadline,bo.getHeadline());
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 更新状态值
     * @param candidateId 主键
     * @param status 状态
     */
    @Override
    public void updateStatus(Long candidateId, String status) {
        List<Long> list = new ArrayList<>();
            list.add(candidateId);
        updateStatus(list, status);
    }

    /**
     * 更新状态值
     * @param ids 主键
     * @param status 0为未导入，1为已导入
     * @return 更新结果
     */
    private Boolean updateStatus(List<Long> ids, String status) {
        return baseMapper.update(new LambdaUpdateWrapper<WbCandidateInfo>().set(WbCandidateInfo::getStatus,status)
            .in(WbCandidateInfo::getId,ids))> 0;
    }
}
